package com.blacktry.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blacktry.entity.model.MultiQuestion;
import com.blacktry.entity.model.MultiQuestion;
import com.blacktry.mapper.MultiQuestionMapper;
import com.blacktry.service.MultiQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 选择题题库表 服务实现类
 * </p>
 *
 * @author blackTry
 * @since 2021-08-10
 */
@Service
public class MultiQuestionServiceImpl extends ServiceImpl<MultiQuestionMapper, MultiQuestion> implements MultiQuestionService {
    @Override
    public int insert(MultiQuestion multiQuestion) {
        return this.baseMapper.insert(multiQuestion);
    }

    @Override
    public Integer delete(Integer id) {
        return this.baseMapper.deleteById(id);
    }

    @Override
    public Integer deleteBatchIds(String[] ids) {
        return this.baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public Integer updateOrInsert(MultiQuestion multiQuestion) {
        if(StrUtil.isBlank(String.valueOf(multiQuestion.getId()))){
            // 没有传入ID直接插入数据
            return this.baseMapper.insert(multiQuestion);
        }else{
            if(select(multiQuestion.getId()) == null){
                // 导入数据的ID没有与表中的ID重复则插入数据
                return this.baseMapper.insert(multiQuestion);
            }else{
                // 传入的ID与表中已有的ID重复则更新数据
                return this.baseMapper.updateById(multiQuestion);
            }
        }
    }

    @Override
    public MultiQuestion select(Integer id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public List<MultiQuestion> selectAll() {
        return this.baseMapper.selectList(null);
    }

    @Override
    public Page<MultiQuestion> selectPage(MultiQuestion multiQuestion, Page<MultiQuestion> multiQuestionPage) {
        LambdaQueryWrapper<MultiQuestion> wrapper = new LambdaQueryWrapper<>();
        // 根据禁用条件查询
        return this.baseMapper.selectPage(multiQuestionPage,wrapper);
    }

    @Override
    public List<MultiQuestion> findByIdAndType(Integer paperId) {
        return this.baseMapper.findByIdAndType(paperId);
    }
}
